using Microsoft.AspNetCore.Mvc;
using MinimalistFoodStreet.BackEnd.Infrastructure.Attributes;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Request.Functions;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Request.Tables;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Response;
using MinimalistFoodStreet.BackEnd.Server.IServer.Tables;

namespace MinimalistFoodStreet.BackEnd.Api.Api.Controllers.Tables
{
    public class GoodsController : BaseController
    {
        private readonly IGoodsServer _iGoodsServer;

        public GoodsController(IGoodsServer iGoodsServer)
        {
            _iGoodsServer = iGoodsServer;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [CheckLogin(new CheckLoginRoles[] {CheckLoginRoles.administrators})]
        [ActionLog("添加商品")]
        public async Task<IActionResult> Add([FromForm] RequestCreateGoodsDto dto)
        {
            return Send(await _iGoodsServer.AddAsync(dto));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [CheckLogin(new CheckLoginRoles[] { CheckLoginRoles.administrators })]
        [ActionLog("修改商品")]
        public async Task<IActionResult> Update([FromForm] RequestUpdateGoodsDto dto)
        {
            return Send(await _iGoodsServer.UpdateAsync(dto));
        }

        /// <summary>
        /// 商品上下架,0下架,1上架
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [CheckLogin(new CheckLoginRoles[] { CheckLoginRoles.administrators,CheckLoginRoles.suppliers })]
        public async Task<IActionResult> SetIsShelve(SetIsShelveDto dto)
        {
            return Send(await _iGoodsServer.SetIsShelve(dto));
        }

        /// <summary>
        /// 设置置顶级别
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [CheckLogin(new CheckLoginRoles[] { CheckLoginRoles.administrators,CheckLoginRoles.suppliers })]
        public async Task<IActionResult> SetTopLevel(SetTopLevelDto dto)
        {
            return Send(await _iGoodsServer.SetTopLevel(dto));
        }

        /// <summary>
        /// 补货
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [CheckLogin(new CheckLoginRoles[] { CheckLoginRoles.administrators,CheckLoginRoles.suppliers })]
        public async Task<IActionResult> Replenishment(ReplenishmentDto dto)
        {
            return Send(await _iGoodsServer.Replenishment(dto));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [CheckLogin(new CheckLoginRoles[] { CheckLoginRoles.administrators})]
        [ActionLog("删除商品")]
        public async Task<IActionResult> Delete(RequestDeleteGoodsDto dto)
        {
            return Send(await _iGoodsServer.DeleteAsync(dto));
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [CheckLogin(new CheckLoginRoles[] {CheckLoginRoles.administrators, CheckLoginRoles.suppliers,CheckLoginRoles.business})]
        public async Task<IActionResult> Detail(int id)
        {
            return Send(await _iGoodsServer.DetailAsync(id));
        }

        /// <summary>
        /// 获取类别编号和数量
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [CheckLogin(new CheckLoginRoles[] {CheckLoginRoles.administrators, CheckLoginRoles.suppliers,CheckLoginRoles.business})]
        public async Task<IActionResult> GetGoodTypeCount(int? sid)
        {
            return Send(await _iGoodsServer.GetGoodTypeCount(sid!=null?(int)sid:_supplier.SId));
        }

        /// <summary>
        /// 获取所有商品类别
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [CheckLogin(new CheckLoginRoles[] {CheckLoginRoles.administrators, CheckLoginRoles.suppliers,CheckLoginRoles.business})]
        public async Task<IActionResult> GetGoodType()
        {
            return Send(await _iGoodsServer.GetGoodType());
        }


        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="page">当前是第几页</param>
        /// <param name="limit">一页有多少条数据</param>
        /// <param name="key">要查询的关键字</param>
        /// <param name="sid">要查询的供应商编号</param>
        /// <param name="gtid">要查询的商品类别编号</param>
        /// <param name="isShelve">要查询的商品是否上架</param>
        /// <returns></returns>
        [HttpGet]
        [CheckLogin(new CheckLoginRoles[] {CheckLoginRoles.administrators, CheckLoginRoles.suppliers,CheckLoginRoles.business})]
        public async Task<IActionResult> Pagination(int page, int limit, string? key, int? sid, int? gtid, int? isShelve)
        {
            return Send(await _iGoodsServer.PaginationAsync(page, limit, key, sid, gtid, isShelve));
        }
        /// <summary>
        /// 商户端获取商品根据热度
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>ss
        [HttpGet]
        [CheckLogin(new CheckLoginRoles[] {CheckLoginRoles.administrators, CheckLoginRoles.suppliers,CheckLoginRoles.business})]
        public async Task<IActionResult> GetGoodHot(int page, int limit)
        {
            return Send(await _iGoodsServer.GetGoodHot(page, limit));
        }
        /// <summary>
        /// 商户端获取活动中的商品
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        [CheckLogin(new CheckLoginRoles[] {CheckLoginRoles.administrators, CheckLoginRoles.suppliers,CheckLoginRoles.business})]
        public async Task<IActionResult> GetGoodPromote(int page, int limit)
        {
            return Send(await _iGoodsServer.GetGoodPromote(page, limit));
        }
        /// <summary>
        /// 商户端分页获得商品
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="gtid"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        [CheckLogin(new CheckLoginRoles[] {CheckLoginRoles.administrators, CheckLoginRoles.suppliers,CheckLoginRoles.business})]
        public async Task<IActionResult> GetGoodPagnation(int page, int limit, int? gtid, string? key)
        {
            return Send(await _iGoodsServer.GetGoodPagnation(page, limit, gtid, key));
        }
        /// <summary>
        /// 商户端获取商品详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [CheckLogin(new CheckLoginRoles[] {CheckLoginRoles.administrators, CheckLoginRoles.suppliers,CheckLoginRoles.business})]
        public async Task<IActionResult> GetGoodDetail(int id)
        {
            return Send(await _iGoodsServer.GetGoodDetail(id,_business.BId));
        }

    }
}
